var rsa = new RSAKey();
rsa.setPublic(
		'a8985afc27ae04d2d529ac7a6624252d5e9550552978e71643a994337c13f7e3869bcb31ecf00a28d4eeb1f875449288f3b1a3bda564c63904d3708199701694747cc53b3d8a3e09a6edfa63d6dad30ea09ffeafe59e2a30b2d8338ff3d82b66c0e6e1ef090dc4d8d1d38eb832737f5994078f1c0a05ab1d6de158d05b92eb7b',
		'10001');

//function encrypt(content) {
//	var contentLength = content.length;
//	var offset = 0;
//	var result = '';
//	while (contentLength - offset > 0) {
//		if (contentLength - offset > 117) {
//			result += rsa.encrypt(content.substr(offset, 117));
//		} else {
//			result += rsa.encrypt(content
//					.substr(offset, contentLength - offset));
//		}
//
//		offset += 117;
//	}
//
//	return hex2b64(result);
//}

function encrypt(content) {
	var contentLength = content.length;
	var offset = 0;
	var currentContent = '';
	var currentResult = '';
	var result = '';
	while (contentLength - offset > 0) {
		if (contentLength - offset > 117) {
			currentContent = content.substr(offset, 117);
		} else {
			currentContent = content.substr(offset, contentLength - offset);
		}
		
		currentResult = rsa.encrypt(currentContent);
		result += Array(257 - currentResult.length).join('0') + currentResult;
		offset += 117;
	}

	return hex2b64(result);
}

function decrypt(content) {
	var hexContent = b64tohex(content);
	var contentLength = hexContent.length;
	var offset = 0;
	var currentContent = '';
	var currentResult = '';
	var result = '';
	while (contentLength - offset > 0) {
		if (contentLength - offset > 256) {
			currentContent = hexContent.substr(offset, 256);
		} else {
			currentContent = hexContent.substr(offset, contentLength - offset);
		}

		currentResult = rsa.decrypt(currentContent);
		result += currentResult;
		offset += 256;
	}

	return result;
}

/*
Array.prototype.distinct = function() {
	var self = this;
	var _a = this.concat().sort();
	_a.sort(function(a, b) {
		if (a == b) {
			var n = self.indexOf(a);
			self.splice(n, 1);
		}
	});
	return self;
};
*/

function setCollectionVisible(scope, collectionName, index) {
	var collection = scope[collectionName];
	var length = collection.length;
	for (var i = 0; i < length; i++) {
		if (i == index) {
			collection[i] = true;
		} else {
			collection[i] = false;
		}
	}
}

function processReturn(content) {
	//return content.replace(/\r\n/g, "\u200c").replace(/\r/g, "\u200c").replace(/\n/g, "\u200c").replace(/\u200c/g, "\r\n");
	if (content == null) {
		return content;
	}

	return content.replace(/([^\r]|^)\n/g, "$1\r\n").replace(/\r([^\n]|$)/g, "\r\n$1");
}

function processDate(date, str, confirmService){
	if(!date){
		confirmService.show(str + ' required!', false, null, null);
		return false;
	}
	
	var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/g;
	if(!reg.test(date)){
		confirmService.show(str + ' format error! Correct format is like 2016-01-01!', false, null, null);
		return false;
	}
	else{
		return true;
	}
}

function processDatetime(datetime, str, confirmService){
	if(!datetime){
		confirmService.show(str + ' required!', false, null, null);
		return false;
	}
	
	var reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
	if(!reg.test(datetime)){
		confirmService.show(str + ' format error! Correct format is like 2016-01-01 00:00:00!', false, null, null);
		return false;
	}
	else{
		return true;
	}
}

function processDigit(digit, str, confirmService){
	if(!digit){
		confirmService.show(str + ' required!', false, null, null);
		return false;
	}
	
	var reg = /^\d+$/g;
	if(!reg.test(digit)){
		confirmService.show(str + ' invalid!', false, null, null);
		return false;
	}
	else{
		return true;
	}
}

function processAlphabet(alphabet, str, confirmService){
	if(!alphabet){
		confirmService.show(str + ' required!', false, null, null);
		return false;
	}
	
	var reg = /^\D+$/g;
	if(!reg.test(alphabet)){
		confirmService.show(str + ' invalid!', false, null, null);
		return false;
	}
	else{
		return true;
	}
}

function machineCallback(machine) {
	return machine.machine != undefined && machine.machine.indexOf('OQC') == 0;
}

function generalCallback() {
	return false;
}

function generalFilter(entities, callback, conditions) {
	var returnEntities = [];
	for ( var index in entities) {
		var entity = entities[index];
		if (callback(entity)) {
			continue;
		}

		var shouldContinue = false;
		for ( var prop in conditions) {
			var conditionProp = conditions[prop];
			var entityProp = entity[prop];
			if (conditionProp != undefined
				&& conditionProp != ""
				&& (entityProp == undefined
					|| (angular.isNumber(entityProp) && entityProp != conditionProp)
					|| (angular.isString(entityProp) && entityProp.indexOf(conditionProp) == -1))) {
				shouldContinue = true;
				break;
			}
		}

		if (shouldContinue) {
			continue;
		}

		returnEntities.push(entity);
	}

	return returnEntities;
}

function auth($q, authenticationService) {
	var userInfo = authenticationService.getUserInfo();

	if (userInfo) {
		return $q.when(userInfo);
	} else {
		return $q.reject({
			authenticated : false
		});
	}
}

function parseDate(datetime) {
	return Date.parse(datetime.replace(/-/g, "/"));
}

var scatterOptions = {
	showScale : true,
	scaleShowLabels : true,
	scaleShowHorizontalLines : true,
	scaleShowVerticalLines : true,
	scaleLineWidth : 1,
	scaleLabel : "<%=value%>",
	scaleDateFormat : "dd/mm/yyyy",
	scaleTimeFormat : "HH:MM",
	scaleDateTimeFormat : "dd/mm/yyyy",
	scaleGridLineWidth : 1,
	useUtc : true,
	pointDot : true,
	scaleType : 'date',
	animation : true,

	scaleOverride : true,
	scaleSteps : 0,
	scaleStepWidth : 0,
	scaleStartValue : 0,

	xScaleOverride : false,
	xScaleSteps : 10,

	// Number - The value jump in the hard coded x scale
	xScaleStepWidth : 3600000 * 24 * 14,

	// Number - The x scale starting value
	xScaleStartValue : 0
}

if(typeof Resource == "undefined"){
	var Resource = {
			Machine: 1,
			OQCMachine: 2,
			Log: 3,
			Sample: 4,
			Calibration : 5,
			QualiyControl : 6,
			
			Statistic : 20,
			StatisiticCalibration : 21,
			StatisiticQualityControl : 22,
			StatisiticStrip : 23,
			StatisiticError : 24,
			StatisiticMcgsError : 25,
			StatisiticUsage : 26,
			StatisiticAging : 27,
			StatisiticTip : 28,
			StatisiticCmdRunTime : 29,
			StatisiticUserCancel : 30,
			
			LotParam : 50,
			ResultDistribution : 51,
			
			Engineer : 70,
			Salesperson : 71,
			agent : 72,
			hospital : 73,
			
			Maintenance : 80
			}
}

var SAMPLEQUERY = 10000;

var LOGQUERY = 300;

var app = angular.module('Quality', [ 'ngRoute', 'ngResource']);